//imageTools.js
const imagemin = require('imagemin'),
      imageminMozjpeg = require('imagemin-mozjpeg'),
      imageminPngquant = require('imagemin-pngquant'),
      path = require('path'),
      fs = require('fs');

function min(target,opt){
   return imagemin(target, {
        plugins: [
            imageminMozjpeg({
                quality:opt.quality||80
            }),
            imageminPngquant({quality: opt.quality||80})
        ]
    });
}

function minOut(target,opt){
   return imagemin(target,opt.output, {
        plugins: [
            imageminMozjpeg({
                quality:opt.quality||80
            }),
            imageminPngquant()
        ]
    });
}

function isArray(arr){
    if(arr instanceof Array){
        return true;
    }else{
        return false;
    }
}

module.exports = function(params){
    if(!params.path){
        return;
    }
    //目前只压缩就够了
    var rootP = process.cwd();
    var opt = {};
    let src = [path.resolve(rootP,params.path)];
    opt.output = path.resolve(rootP,params.output||'output');
    fs.stat(src.toString(), (err,st)=>{
        if(err){
            console.log(err);
            return;
        }
        if(!st){
            console.log('directory or file is not exist')
        }
        if(st.isDirectory()){
            src += '/**';
        }
        console.log(opt);
        let task = minOut([src],opt);
    });
}